/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Login.java
 *
 * Created on Nov 16, 2010, 9:47:31 AM
 */
package lightweighttransferagent;

import java.io.IOException;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import org.globus.ftp.FTPClient;
import org.globus.ftp.exception.ServerException;

/**
 * This is a GUI for login.
 * This class provide login function for user.
 * 
 * @author diluka
 */
public class Login extends javax.swing.JDialog {

    private FTPClient client;
    private MainView parent;

    public Login(MainView parent, boolean modal) {
        super(parent, modal);
        this.parent = parent;
        try {
            //to get OS look
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        //set to center
        this.setLocationRelativeTo(parent);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextFieldUserName = new javax.swing.JTextField();
        jPasswordField = new javax.swing.JPasswordField();
        jButtonLogin = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Login");

        jLabel1.setText("User Name :");

        jLabel2.setText("Passward   :");

        jTextFieldUserName.setText("anonymous");

        jPasswordField.setText("anonymous");

        jButtonLogin.setText("Login");
        jButtonLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonLoginActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButtonLogin)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addGap(33, 33, 33)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jPasswordField)
                            .addComponent(jTextFieldUserName, javax.swing.GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextFieldUserName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jPasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jButtonLogin)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonLoginActionPerformed
        String username = jTextFieldUserName.getText();
        String password = jPasswordField.getPassword().toString();
        boolean ok = true;

        try {
            //creating org.globus.ftp.FTPClient object to creat a socket to server
            client = new FTPClient("localhost", 5000);
        } catch (IOException ex) {
            System.out.println("IO exception connecting to server: " + ex.getMessage());
            ok = false;
        } catch (ServerException ex) {
            System.out.println("Server exception connecting to server: " + ex.getMessage());
            ok = false;
        } catch (Exception ex) {
            System.out.println("Error connecting to server: " + ex.getMessage());
            ok = false;
        }

        if (ok) {
            try {
                //authorize the user with the server
                getClient().authorize(username, password);
            } catch (IOException ex) {
                System.out.println("IO exception login: " + ex.getMessage());
                ok = false;
            } catch (ServerException ex) {
                System.out.println("Server exception login: " + ex.getMessage());
                ok = false;
            } catch (Exception ex) {
                System.out.println("Error login: " + ex.getMessage());
                ok = false;
            }
            if (ok) {
                //if authorizing is success
                JOptionPane.showMessageDialog(this, "Successfully login.", "Login", JOptionPane.INFORMATION_MESSAGE);
                parent.setDefault(true);
                this.setVisible(false);
            } else {
                //if authorizing fail
                JOptionPane.showMessageDialog(this, "Error while login.", "Error", JOptionPane.ERROR_MESSAGE);
            }

        } else {
            //if fail to creat client object
            JOptionPane.showMessageDialog(this, "Error while connecting to server.", "Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jButtonLoginActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                Login dialog = new Login((MainView) new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonLogin;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPasswordField jPasswordField;
    private javax.swing.JTextField jTextFieldUserName;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the client
     */
    public FTPClient getClient() {
        return client;
    }
}
